package com.tgy.树.二叉树;

/***
 * @ClassName: BinaryTree
 * @Description: 二叉树
 * @Auther: tanggy3
 * @Date: 15:43 2022/12/22
 * @version : V1.0
 */
//定义BinaryTree 二叉树
public class BinaryTree {
    private Node root;
    private int count = 0;

    public void setRoot(Node root) {
        this.root = root;
    }


    /**
     * 三种遍历
     */
    //调用节点的前序遍历
    public void preOrder() {
        if (this.root != null) {
            this.root.preOrder();
        } else {
            System.out.println("二叉树为空，无法遍历");
        }
    }

    //调用节点的中序遍历
    public void midOrder() {
        if (this.root != null) {
            this.root.midOrder();
        } else {
            System.out.println("二叉树为空，无法遍历");
        }
    }

    //调用节点的后序遍历
    public void postOrder() {
        if (this.root != null) {
            this.root.postOrder();
        } else {
            System.out.println("二叉树为空，无法遍历");
        }
    }

    /**
     * 三种查找
     */

    public void quickFind(Node targetNode) {
        if (this.root != null) {
            this.root.quickFind(targetNode);
        } else {
            System.out.println("二叉树为空，无法遍历");
        }
    }

    public Node preFind(int targetNode) {
        Node returnNode = null;
        if (this.root != null) {
            returnNode = this.root.preFind(targetNode);
        } else {
            System.out.println("二叉树为空，无法遍历");
        }
        return returnNode;
    }

    public Node midFind(int targetNode) {
        Node returnNode = null;
        if (this.root != null) {
            returnNode = this.root.midFind(targetNode);
        } else {
            System.out.println("二叉树为空，无法遍历");
        }
        return returnNode;

    }

    public Node postFind(int targetNode) {
        Node returnNode = null;
        if (this.root != null) {
            returnNode = this.root.postFind(targetNode);
        } else {
            System.out.println("二叉树为空，无法遍历");
        }
        return returnNode;
    }


    public void delNode(int targetNo){
        if(this.root ==null){
            System.out.println("二叉树为空，无法删除");
        }else {
            if (this.root.no == targetNo){
                root = null;
                System.out.println("目标为根节点，删除成功");
            }else {
                boolean b = this.root.delNode(targetNo);
                if(b){
                    System.out.println("删除成功");
                }else {
                    System.out.println("未找到");
                }
            }
        }
    }

}
